//
//  ChangePassword.m
//  YaoHeAPP
//
//  Created by mac on 15/12/31.
//  Copyright © 2015年 mac. All rights reserved.
//

#import "ChangePassword.h"
#import "Controller.h"
#import "Define.h"
#import "Masonry.h"
@interface ChangePassword ()
{
    UIView*backView;
    UITextField*passWord;
}
@end

@implementation ChangePassword

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"ChangePassword"];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"ChangePassword"];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.view.backgroundColor=bgVCcolor;
    //加一个导航条
    UIView*navcBar=[[UIView alloc]init];
    navcBar.backgroundColor=[UIColor colorWithRed:90.f/255.f green:191.f/255.f blue:219.f/255.f alpha:1];
    [self.view addSubview:navcBar];
    [self.view bringSubviewToFront:navcBar];
    [navcBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);   make.left.offset(0);
        make.height.offset(kNavigationBarHeight);  make.right.offset(0);
    }];
    //导航条标题
    UILabel*titleLabel=[[UILabel alloc]init];
    titleLabel.text=@"修改密码";
    titleLabel.font=Font(17);
    titleLabel.textAlignment=NSTextAlignmentCenter;
    titleLabel.textColor=[UIColor whiteColor];
    [navcBar addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(25);
        make.left.offset(self.view.frame.size.width/2-50);
        make.width.offset(100);
        make.height.offset(30);
    }];
    
    //返回按钮
    UIButton*leftButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setBackgroundImage:[UIImage imageNamed:@"return"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [navcBar addSubview:leftButton];
    [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(6);
        make.width.offset(24);
        make.height.offset(22);
        make.bottom.offset(-12);
    }];
    
    
    NSArray*labelArray=@[@"当前密码:",@"新  密  码:",@"确认密码:"];
    NSArray*placeArray=@[@"请输入6~20位数字或字母",@"请输入6~20位数字或字母",@"请再次输入一次密码"];
    for (int i=0; i<3 ;i++)
    {
        backView=[[UIView alloc]init];
        backView.backgroundColor=[UIColor whiteColor];
        [self.view addSubview:backView];
        [backView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(navcBar.mas_bottom).offset(i*51+5);
            make.left.offset(0);
            make.right.offset(0);
            make.height.offset(50);
        }];
        
        //必填的内容前的星星图片
        UIImageView*startImage=[[UIImageView alloc]init];
        startImage.image=[UIImage imageNamed:@"bi_tian"];
        [backView addSubview:startImage];
        [startImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(17);
            make.left.offset(5);
            make.height.offset(16);
            make.width.offset(17);
        }];
        
        //提示标签
        UILabel*phone=[[UILabel alloc]init];
        phone.font=[UIFont systemFontOfSize:15];
        phone.text=labelArray[i];
        phone.textAlignment=NSTextAlignmentCenter;
        [backView addSubview:phone];
        [phone mas_makeConstraints:^(MASConstraintMaker*make) {
            make.left.offset(28);
            make.top.offset(6);
            make.bottom.offset(-6);
        }];
        
        //手机号输入框
        passWord=[[UITextField alloc]init];
        //passWord.clearsOnBeginEditing=YES;
        passWord.clearButtonMode=UITextFieldViewModeWhileEditing;
        passWord.font=[UIFont systemFontOfSize:14];
        passWord.placeholder=placeArray[i];
        passWord.borderStyle=UITextBorderStyleNone;
        passWord.tag=50+i;
        passWord.secureTextEntry=YES;
        [passWord addTarget:self action:@selector(keyboardDissmiss) forControlEvents:UIControlEventEditingDidEndOnExit];
        [backView addSubview:passWord];
        [passWord mas_makeConstraints:^(MASConstraintMaker *make) {
            //make.left.equalTo(phone.mas_right).offset(2);
            make.left.offset(100);
            make.right.offset(-6);
            make.top.offset(6);
            make.bottom.offset(-6);
        }];
    }
    
    //下一步
    UIButton*nextBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [nextBtn setTitle:@"确认修改" forState:UIControlStateNormal];
    UIImage*loginOldImg=[UIImage imageNamed:@"loginbutton"];
    UIImage*loginNewImg=[loginOldImg stretchableImageWithLeftCapWidth:15 topCapHeight:0];
    [nextBtn setBackgroundImage:loginNewImg forState:UIControlStateNormal];
    [nextBtn addTarget:self action:@selector(nextBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:nextBtn];
    [nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(backView.mas_bottom).offset(20);
        make.left.offset(50);
        make.right.offset(-50);
        make.height.offset(40);
    }];

    
// Do any additional setup after loading the view.
}

-(void)nextBtn
{
    UITextField*field1=(UITextField*)[self.view viewWithTag:50];
    UITextField*field2=(UITextField*)[self.view viewWithTag:51];
    UITextField*field3=(UITextField*)[self.view viewWithTag:52];
    if (field1.text.length<6||field1.text.length>20) {
        ALERTSHOW(@"请输入正确的原密码");
        return;
    }
    if ([field2.text isEqualToString:field1.text]) {
        ALERTSHOW(@"新密码不能与原来的密码重复");
        return;
    }
    
    if(field2.text.length==0)
    {
        ALERTSHOW(@"请输入新密码");
        return;
    }
    if (field2.text.length<6||field2.text.length>20) {
        ALERTSHOW(@"请输入6~20位数字或字母");
        return;
    }
    if (![field2.text isEqualToString:field3.text]) {
        ALERTSHOW(@"两次的新密码输入不一致,请重新输入");
        return;
    }
    
NSDictionary*dic=@{@"OLD_PASSWORD":field1.text,@"NEW_PASSWORD":field2.text};
    
    [Controller requestWithURL:@"AppUserCenter/PostSavePassword?from=app" jsondic:dic finishBlock:^(ClassData *classData) {
        if ([classData.number integerValue]==0) {
              UIAlertView*alertView=[[UIAlertView alloc]initWithTitle:@"提示" message:@"恭喜你重置密码成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
            alertView.tag=100;
            [alertView show];
        }
        else{
            ALERTSHOW(classData.message);
        }
    }];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag==100) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

//键盘弹下
-(void)keyboardDissmiss
{
    return;
}
//键盘弹下
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}





-(void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
